package com.bizframework.core.domain;

import com.bizframework.core.util.DateUtils;
import com.bizframework.core.util.StringUtils;

public class QueryCondition implements Cloneable {
	private QueryLogic queryLogic = QueryLogic.And;

	private String fieldName;

	private String queryOperator;

	private Object fieldValue;

	private String fieldValueString;

	private FieldType fieldType;

	public Object getFieldValue() {
		return fieldValue;
	}

	public void setFieldValue(Object fieldValue) {
		this.fieldValue = fieldValue;
	}

	public FieldType getFieldType() {
		return fieldType;
	}

	public void setFieldType(FieldType fieldType) {
		this.fieldType = fieldType;
	}

	public String getFieldValueString() {
		return fieldValueString;
	}

	public void setFieldValueString(String fieldValueString) {
		this.fieldValueString = fieldValueString;
	}

	public QueryLogic getQueryLogic() {
		return queryLogic;
	}

	public void setQueryLogic(QueryLogic queryLogic) {
		this.queryLogic = queryLogic;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getQueryOperator() {
		return queryOperator;
	}

	public void setQueryOperator(String queryOperator) {
		this.queryOperator = queryOperator;
	}

	public QueryCondition() {
	}

	/**
	 * 构造函数
	 * 
	 * @param queryLogic
	 *            查询逻辑。可以通过QueryLogic枚举值获取
	 * @param fieldName
	 *            查询字段。
	 * @param queryOperator
	 *            查询操作符。可通过QueryOperator获取相应字符，也可自己编写。 例如：等于可以写成{0}={1}。
	 * @param fieldValueString
	 * @param fieldType
	 */
	public QueryCondition(QueryLogic queryLogic, String fieldName,
			String queryOperator, String fieldValueString, FieldType fieldType) {
		this.queryLogic = queryLogic;
		this.fieldName = fieldName;
		this.queryOperator = queryOperator;
		this.fieldValueString = fieldValueString;
		this.fieldType = fieldType;
	}

	public String toString() {
		return this.queryLogic + " " + this.fieldName + " "
				+ this.queryOperator + " " + this.fieldValueString;
	}

	public QueryCondition clone() {
		QueryCondition qc = new QueryCondition();
		qc.fieldName = fieldName;
		qc.fieldValueString = fieldValueString;
		qc.queryLogic = queryLogic;
		qc.queryOperator = queryOperator;
		qc.fieldType = fieldType;
		return qc;
	}

	public Object parseFieldValueString() throws Exception {
		if (this.fieldValueString == null) {
			return null;
		}
		switch (this.fieldType) {
		case Date:
			if (StringUtils.isNullOrEmpty(fieldValueString)) {
				return null;
			}
			return DateUtils.StringToDate(this.fieldValueString);
		case DateTime:
			if (StringUtils.isNullOrEmpty(fieldValueString)) {
				return null;
			}
			return DateUtils.StringToDateTime(this.fieldValueString);
		case Integer:
			if (StringUtils.isNullOrEmpty(fieldValueString)) {
				return null;
			}
			return Integer.parseInt(this.getFieldValueString());
		case Float:
			if (StringUtils.isNullOrEmpty(fieldValueString)) {
				return null;
			}
			return Float.parseFloat(this.getFieldValueString());
		case Boolean:
			return Boolean.parseBoolean(fieldName);
		default:
			return this.fieldValueString;
		}
	}
}